package net.myspring.common.domain;

import com.google.common.collect.Lists;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by liuj on 2016/10/4.
 */
public class HttpClientEntity {
    private Logger logger = LoggerFactory.getLogger(getClass());

    private String httpUrl;
    private String userAgent;
    private List<BasicClientCookie> basicClientCookies = Lists.newArrayList();
    private Integer connectTimeout = 100000;
    private HttpEntity httpEntity;

    private Map<String,String> paramMap;

    public HttpClientEntity() {

    }

    public HttpClientEntity(String httpUrl) {
        this.httpUrl = httpUrl;
    }

    public HttpClientEntity(String httpUrl, String userAgent) {
        this.httpUrl = httpUrl;
        this.userAgent = userAgent;
    }

    public String getHttpUrl() {
        return httpUrl;
    }

    public void setHttpUrl(String httpUrl) {
        this.httpUrl = httpUrl;
    }

    public String getUserAgent() {
        return userAgent;
    }

    public void setUserAgent(String userAgent) {
        this.userAgent = userAgent;
    }

    public List<BasicClientCookie> getBasicClientCookies() {
        return basicClientCookies;
    }

    public void setBasicClientCookies(List<BasicClientCookie> basicClientCookies) {
        this.basicClientCookies = basicClientCookies;
    }

    public Integer getConnectTimeout() {
        return connectTimeout;
    }

    public void setConnectTimeout(Integer connectTimeout) {
        this.connectTimeout = connectTimeout;
    }

    public HttpEntity getHttpEntity() {
        return httpEntity;
    }

    public void setHttpEntity(HttpEntity httpEntity) {
        this.httpEntity = httpEntity;
    }

    public Map<String, String> getParamMap() {
        return paramMap;
    }

    public void setParamMap(Map<String, String> paramMap) {
        this.paramMap = paramMap;
    }

    public UrlEncodedFormEntity getUrlEncodedFormEntity() {
        List<NameValuePair> list = Lists.newArrayList();
        UrlEncodedFormEntity urlEncodedFormEntity = null;
        if(paramMap != null && paramMap.size()>0) {
            list.addAll(paramMap.keySet().stream().map(key -> new BasicNameValuePair(key, paramMap.get(key))).collect(Collectors.toList()));
            try {
                urlEncodedFormEntity= new UrlEncodedFormEntity(list,"utf-8");
            } catch (UnsupportedEncodingException e) {
                logger.error(e.getMessage());
            }

        }
        return urlEncodedFormEntity;
    }

}
